﻿$(document).ready(function () {
    //初始化对象
    var m_cksqdj = new SmObjectData("CKQDJK", "采矿申请登记");
    var m_metaconst = new SmConstClsList();
    //获取流程号PROJECT_ID	
    var m_project_id = $.query.get("Project_ID");
    var SQLX = $.query.get("SQLX");
    if (SQLX != "1") {
        $(".m_btn").css("display", "none");
    }
    //获取一条记录
    m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
    //根据矿种，初始化储量单位和储量规模单位
    $("#txtKZDM").change(function () {
        InitCLDW("txtKZDM", "sel规模单位", "selCLDW");
    });
    //总储量改变，综合总储量单位，计算储量规模
    $("#txt总储量").change(function () {
        InitCLGM("txtCLGM", "txtKZDM", "txt总储量", "selCLDW");
    });

  


    //给申请页面赋值
    $(":text").each(function () {
        var ff = $(this).attr("name");
        if (ff == "开采主矿种" || ff == "总储量") {
            $(this).val(m_cksqdj.GetValue(ff));
            if (ff == "开采主矿种") {
                FireChangeEvent("txtKZDM");
                $("#txtKZMC").val(GetKZMCbyKZDM(m_cksqdj.GetValue(ff)));
            }
            else if (ff == "总储量") {
                FireChangeEvent("txt总储量");
            }
        }
        else {
            $(this).val(m_cksqdj.GetValue(ff));
        }
    });
    $("textarea").each(function () {
        var dd = $(this).attr("name");
        $(this).val(m_cksqdj.GetValue(dd));
    });
    $("select").each(function () {
        var ss = $(this).attr("name");
        var mc = m_cksqdj.GetValue(ss);
        if (ss == "储量单位") {//储量单位
            for (var i = 0; i < this.options.length; i++) {
                var m_dw = mc + " " + m_cksqdj.GetValue("KSLX");
                if (m_dw == this.options[i].innerHTML) {//$("#"+this.id +" option:selected").attr("dw")){
//                    this.options[i].selected = true;
                }
            }
        }
        else if (ss == "ZCZJDW" || ss == "规模单位") {//注册资金单位和规模单位
            for (var i = 0; i < this.options.length; i++) {
                if (mc == this.options[i].innerHTML) {
                    this.options[i].selected = true;
                }
            }
        }
    });
    var gbs = m_cksqdj.GetValue("GBSLX");
    gbs == 1 ? $("#txtGBS").attr("checked", false) : $("#txtGBS").attr("checked", true);
    //给下拉列表初始化并赋值
    m_metaconst.Init("KCK_JJLX,CKQQDFS,KCK_ZJLY,KCK_KCFS,KCK_CKFS,KCK_XKFF,KCK_JKCZFS");
    m_metaconst.FillSelectHtml($("#sel经济类型"), "KCK_JJLX", m_cksqdj.GetValue("经济类型"));
    m_metaconst.FillSelectHtml2($("#cmdCKHDFS"), "CKQQDFS", m_cksqdj.GetValue("采矿权取得方式"));
    m_metaconst.FillSelectHtml4($("#sel资金来源"), "KCK_ZJLY", m_cksqdj.GetValue("资金来源"));
    m_metaconst.FillSelectHtml4($("#sel开采方式"), "KCK_KCFS", m_cksqdj.GetValue("开采方式")); //开采方式
    m_metaconst.FillSelectHtml4($("#sel采矿方法"), "KCK_CKFS", m_cksqdj.GetValue("采矿方法")); //采矿方法
    m_metaconst.FillSelectHtml4($("#sel选矿方法"), "KCK_XKFF", m_cksqdj.GetValue("选矿方法")); //选矿方法
    m_metaconst.FillSelectHtml($("#sel价款处置方式"), "KCK_JKCZFS", m_cksqdj.GetValue("价款处置方式代码")); //价款处置方式
    //时间控件添加单击事件
    $("#txtQSQX").click(function () {
        WdatePicker({ dateFmt: 'yyyy-M-d' });
    });
    $("#txtZZQX").click(function () {
        WdatePicker({ dateFmt: 'yyyy-M-d' });
    });
    InitCLDW("txtKZDM", "sel规模单位", "selCLDW");
    //保存申请页面信息
    $("#btnSave").click(function () {
        m_cksqdj.SetValue("PROJECT_ID", m_project_id);
        $(":text").each(function () {
            m_cksqdj.SetValue($(this).attr("name"), $(this).val());
        });
        $("textarea").each(function () {
            m_cksqdj.SetValue($(this).attr("name"), $(this).val());
        });
        $("select").each(function () {
            if ($(this).attr("name") == "采矿权取得方式") {
                var m_val = $("#" + $(this).attr("id") + " option:selected").attr("value");
                var m_mc = $("#" + $(this).attr("id") + " option:selected").attr("mc");
                m_cksqdj.SetValue("取得方式代码", m_val == null ? '' : m_val);
                m_cksqdj.SetValue("采矿权取得方式", m_mc == null ? '' : m_mc);
            }
            else if ($(this).attr("name") == "价款处置方式") {
                var m_val = $("#" + $(this).attr("id") + " option:selected").attr("value");
                var m_mc = $("#" + $(this).attr("id") + " option:selected").attr("mc");
                m_cksqdj.SetValue("价款处置方式代码", m_val == null ? '' : m_val);
                m_cksqdj.SetValue("价款处置方式", m_mc == null ? '' : m_mc);
            }
            else if ($(this).attr("name") == "储量单位" || $(this).attr("name") == "规模单位") {
                var m_mc = $("#" + $(this).attr("id") + " option:selected").attr("dw");
                m_cksqdj.SetValue($(this).attr("name"), m_mc == null ? '' : m_mc);
                if ($(this).attr("name") == "储量单位") {
                    var m_sm = $("#" + $(this).attr("id") + " option:selected").attr("sm");
                    m_cksqdj.SetValue("KSLX", m_sm == null ? '' : m_sm);
                };
            }
            else if ($(this).attr("name") == "经济类型") {
                var m_val = $("#" + $(this).attr("id") + " option:selected").attr("value");
                m_cksqdj.SetValue("经济类型", m_val == null ? '' : m_val);
            }
            else if ($(this).attr("name") == "ZCZJDW" || $(this).attr("name") == "TZEDW") {
                var m_val = $("#" + $(this).attr("id") + " option:selected").attr("value");
                m_cksqdj.SetValue($(this).attr("name") + "DM", m_val == null ? '' : m_val);
            }
            else {
                var m_val = $("#" + $(this).attr("id") + " option:selected").attr("value");
                var m_mc = $("#" + $(this).attr("id") + " option:selected").attr("mc");
                m_cksqdj.SetValue($(this).attr("name") + "DM", m_val == null ? '' : m_val);
                m_cksqdj.SetValue($(this).attr("name"), m_mc == null ? '' : m_mc);
            }
            $("#txtGBS").attr("checked") == "checked" ? m_cksqdj.SetValue("GBSLX", 2) : m_cksqdj.SetValue("GBSLX", 1);
        });
        var res = m_cksqdj.Save(DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKSQDJ.asmx/SaveCKQXL", "'LX':'SQ'");
        if (res == true) {
            alertex("保存成功", true);
        }
        else alertex("保存失败", true);
    });
    //选择矿种
    $("#btnKZ").click(function () {
        ShowMetaTree("", SetKZ);
    });
     $("#btnQTKZ").click(function () {
        ShowMetaTree("", SetQTKZ);
    });
    //选择行政区
    $("#btnXZQ").click(function () {
        ShowMetaTree("XZQ", SetXZQ);
    });
    //矿种选择成功后回调函数，给文本框赋值
    function SetKZ(kzdm, kzmc) {
        $("#txtKZMC").val(kzmc);
        $("#txtKZDM").val(kzdm);
        FireChangeEvent("txtKZDM");
    };
    function SetQTKZ(kzdm, kzmc) {
        $("#txt其它主矿种").val(kzmc+"-"+kzdm);
    };
    //行政区选择成功后回调函数，给文本框赋值
    function SetXZQ(xzqdm, xzqmc) {
        $("#txt所在行政区名称").val(xzqmc);
        $("#txtSZXZQDM").val(xzqdm);
    };
});
